package com.tank.demo.entry.tank;

import com.tank.demo.abstractFactory.BaseTank;
import com.tank.demo.entry.bullet.RetroBullet;
import com.tank.demo.facadeAndMediator.GameModel;
import com.tank.demo.frame.TankFrame;
import com.tank.demo.myenum.DirEnum;
import com.tank.demo.myenum.TankGroupEnum;
import com.tank.demo.strategy.FireStrategy;
import com.tank.demo.utils.PropertyMgr;
import com.tank.demo.utils.ResourceMgr;
import lombok.*;

import java.awt.*;
import java.util.Random;

@Setter
@Getter
public class RetroTank extends BaseTank {

    public RetroTank(int x, int y, DirEnum dir, boolean moving, TankGroupEnum group) {
        super(x, y, dir, moving, group);
    }
    public void move() {
        oldX = x;
        oldY = y;
        switch (dir) {
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
        }
    }
    public void goBack(){
        x = oldX;
        y = oldY;
    }
    @Override
    public void paint(Graphics g) {
        switch (dir) {
            case UP:
                g.drawImage(this.group == TankGroupEnum.BLUE ? ResourceMgr.enemyTankU : ResourceMgr.myTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == TankGroupEnum.BLUE ? ResourceMgr.enemyTankD : ResourceMgr.myTankD, x, y, null);
                break;
            case LEFT:
                g.drawImage(this.group == TankGroupEnum.BLUE ? ResourceMgr.enemyTankL : ResourceMgr.myTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == TankGroupEnum.BLUE ? ResourceMgr.enemyTankR : ResourceMgr.myTankR, x, y, null);
                break;
        }
        if (moving) move();
        borderDetection();

        if (group == TankGroupEnum.RED) return;
        if (new Random().nextInt(100) > 95) {
            randomDir();
        }
        moving = new Random().nextInt(100) > 80 ? true : false;

        if (new Random().nextInt(100) > 93) {
            try {
                fire((FireStrategy) Class.forName(PropertyMgr.getInstance().getKey("eneryTankFireStrategy")).newInstance());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void randomDir() {
        setDir(DirEnum.values()[new Random().nextInt(4)]);
    }

    private void borderDetection() {
        if (this.x < 2) this.x = 2;
        if (this.x > GameModel.GAME_WIDTH - this.width) this.x = GameModel.GAME_WIDTH - this.width;
        if (this.y < 32) this.y = 32;
        if (this.y > GameModel.GAME_HEIGHT - this.height) this.y = GameModel.GAME_HEIGHT - this.height;
    }

    public void fire(FireStrategy fireStrategy) {
        fireStrategy.fire(this, GameModel.gameAbstractFactory.createBullet(0, 0, dir, group));
    }

    public void die() {
        GameModel.getInstance().remove(this);
    }
}
